package com.zerous.staria.screens;

import com.zerous.staria.*;
import com.zerous.framework.*;
import com.zerous.framework.ui.*;
import com.zerous.staria.entity.*;
import java.util.*;

public class GScreen extends LayoutScreen
{
	List<Entity> entities;
	Button left;
	Button right;
	Button jump;
	Vector2 gravity;
	boolean landed=false, hitPlane;
	Partical p;
	public GScreen(Game g, Screen par)
	{
		super(g, par);
		bindLayout(Res.gameControler);
		left = (Button)getUI("left");
		right = (Button)getUI("right");
		jump = (Button)getUI("jump");
		//p = new Partical(null, null, null);
		gravity =  new Vector2(0, 0.01);
		p = new Partical(new Vector2(Zex.Const.width/2, Zex.Const.height/2), new Vector2(0, 0), new Vector2(0, 0));
		initButtons();
	}

	@Override
	public void update()
	{
		// TODO: Implement this method
		super.update();
		
		hitPlane = (p.position.getY() + p.velocity.getY() >= Zex.Const.height - 70 && p.position.getY() <= Zex.Const.height - 71) && (p.position.getX() > 500&& p.position.getX()<900);
		if(hitPlane && !landed)
		{
			landed = true;
			p.accel.setY(0);
			p.velocity.setY(0);
			p.position.setY(Zex.Const.height - 70);
		}
		else if(p.position.getY() + p.velocity.getY() >= Zex.Const.height - 20 && !landed)
		{
			landed = true;
			p.accel.setY(0);
			p.velocity.setY(0);
			p.position.setY(Zex.Const.height - 20);
		}
		else
		{
			landed = false;
		}
		
		p.physicalUpdate();
		if(!landed) p.accel.add(gravity);
	}

	@Override
	public void render()
	{
		Zex.graphics.clear(0xffffffff);
		Zex.graphics.drawCircle(p.position, 20);
		drawWigets();
		Zex.graphics.drawRect(500, Zex.Const.height - 70, 400,1);
	}
	
	
	
	void initButtons()
	{
		left.setOnTouch(new Button.OnTouch()
		{
			public void down()
			{
				if(p.velocity.getX() >= -5) p.accel.setX(-0.1);
			}
			
			public void up()
			{
				p.accel.setX(0);
				//Util.toast(game, "Up");
			}
		});
		
		right.setOnTouch(new Button.OnTouch()
		{
			public void down()
			{
				if(p.velocity.getX() <= 5) p.accel.setX(0.1);
			}
			
			public void up()
			{
				p.accel.setX(0);
			}
		});
		
		jump.setOnClick(new Button.OnClick()
		{
			public void click()
			{
				//if(landed) {
					p.velocity.setY(-10);
					landed = false;
				//}
			}
		});
	}
}
